package netty.udp;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import test.PerformanceAnalyze;

import java.time.Instant;

public class ServerHandler extends SimpleChannelInboundHandler<com.zetas.option.push.message.sdk.PushSDKMessage.Message4Backend> {

    static PerformanceAnalyze performanceAnalyze = new PerformanceAnalyze();

    public ServerHandler() {
        System.out.println("ServerHandler create");
    }


    @Override
    protected void channelRead0(ChannelHandlerContext ctx, com.zetas.option.push.message.sdk.PushSDKMessage.Message4Backend msg) throws Exception {

        performanceAnalyze.meter();
        performanceAnalyze.delay(Instant.now().getNano() - msg.getPublishTime());
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx){
//        System.out.println("server 读取数据完毕..");
//        ctx.flush();//刷新后才将数据发出到SocketChannel

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
        cause.printStackTrace();
        ctx.channel().close();
    }

}
